package com.nineclock.sign.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.sign.dto.SignInfoDTO;
import com.nineclock.sign.dto.SignInfoListDTO;
import com.nineclock.sign.dto.UserInfo;
import com.nineclock.sign.dto.UserSignInfoDTO;
import com.nineclock.sign.service.SignInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

/**
 * @author cjh
 * @date 2021/12/2 15:42
 */
@RestController("sign")
@Api(value = "签到服务" ,tags="签到服务")
public class SignInfoController {
    @Autowired
    private SignInfoService signInfoService;

    /**
     * 接口名称：APP: 签到
     * 接口路径：POST/sign/insert
     */
    @ApiOperation("签到")
    @PostMapping("insert")
    public Result insert(SignInfoDTO signInfoDTO) throws UnsupportedEncodingException {
        signInfoService.insert(signInfoDTO);
        return Result.success();
    }

    /**
     * 接口名称：APP: 获取当前用户当天签到次数
     * 接口路径：GET/sign/getDayCount
     */
    @ApiOperation("获取当前用户当天签到次数")
    @GetMapping("getDayCount")
    public Result<Integer> getDayCount(){
        Integer count = signInfoService.getDayCount();
        return Result.success(count);
    }

    /**
     * 接口名称：APP: 获取团队员工打卡列表
     * 接口路径：GET/sign/listSignAllUser
     */
    @ApiOperation("获取团队员工打卡列表")
    @GetMapping("listSignAllUser")
    public Result<List<SignInfoListDTO>> listSignAllUser(String dateStr){
        List<SignInfoListDTO> SignInfoListDTOList = signInfoService.listSignAllUser(dateStr);
        return Result.success(SignInfoListDTOList);
    }


    /**
     * APP: 查询本月签到记录
     * 接口路径：GET/sign/pagingSignCurrentUserByMonth
     * @return
     */
    @GetMapping("pagingSignCurrentUserByMonth")
    public Result<List<SignInfoDTO>> getMonthCount(){
        List<SignInfoDTO> signInfoDTOList = signInfoService.getMonthCount();
        return Result.success(signInfoDTOList);
    }

    /**
     * 接口名称：PC: 查询签到列表
     * 接口路径：GET/sign/sign
     */
    @ApiOperation("查询签到列表")
    @GetMapping("sign")
    public Result<PageResult<UserSignInfoDTO>> sign(
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "7") Integer pageSize){
        PageResult<UserSignInfoDTO> pageResult = signInfoService.sign(startTime,endTime,page,pageSize);
        return Result.success(pageResult);
    }

    /**
     * 接口名称：PC: 导出签到列表
     * 接口路径：GET/sign/export
     */
    @ApiOperation("导出签到列表")
    @GetMapping("export")
    public void export(@RequestParam String startTime,
                       @RequestParam  String endTime) throws IOException {
        signInfoService.export(startTime,endTime);
    }

    /**
     * GET/sign/getById
     */
    @ApiOperation("通过id查询签到数据")
    @GetMapping("getById")
    public Result<SignInfoDTO> getById(Long id){
        SignInfoDTO aignInfoDTO = signInfoService.getById(id);
        return Result.success(aignInfoDTO);
    }

    /**
     * GET sign/92/2021-12-05
     * 根据用户id和日期查询详细信息
     */
    @GetMapping("{userId}/{createDate}")
    public Result<UserInfo> getSignInfoByUserIdAndCreateDate(@PathVariable Long userId, @PathVariable String createDate){
        UserInfo userinfo = signInfoService.getSignInfoByUserIdAndCreateDate(userId,createDate);
        return Result.success(userinfo);

    }
}
